import jstAxios from './jstAxios'
import config from '@/config/setting'
import store from '@/store'
jstAxios.showLoading = config.showLoading
// 设置响应拦截器
jstAxios.interceptor.response = function (data) {
    if (data.code !== 200 && data.code === 101) {
        store.dispatch('user/login')
    } else if (data.code !== 200 && data.code === 102) {
        uni.showToast({
            title: data.msg,
            icon: 'none',
            duration: 3000
        });
        return false
    } else if (data.code !== 200 && data.code === 404) {
        uni.navigateTo({
            url: `/pages/404/404`
        })
    } else if (data.code !== 200 && data.code === 105) {
        uni.showModal({
            title: '提示',
            content: data.msg,
            showCancel: false,
            success: function (res) {
                if (res.confirm) {
                    /* uni.navigateTo({
                        url:`/pages/user/error-institution`
                    }) */
                } else if (res.cancel) {

                }
            }
        });
        return data
    } else {
        return data
    }
}

// 设置请求拦截器
jstAxios.interceptor.request = function (data) {
    //#ifdef H5
    if (window.appConfig.isTest) {
        //data.data.code = 1
        // /data.data.code = 1
    } else {
        // data.data.code = store.state.user.code
        //data.data.openid = store.state.user.openid
    }
    //#endif

    //#ifdef MP-WEIXIN
    // data.data.code = store.state.user.code
    // data.data.test_open=1
    // data.data.token=1
    //#endif
    return data
}

export default jstAxios
